package com.lj.demo.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lj.demo.eduservice.entity.EduSubject;
import com.lj.demo.eduservice.entity.ExcelSubjectData;
import com.lj.demo.eduservice.entity.subject.OneSubject;
import com.lj.demo.eduservice.entity.subject.TwoSubject;
import com.lj.demo.eduservice.listener.EduSubjectExcelLister;
import com.lj.demo.eduservice.mapper.EduSubjectMapper;
import com.lj.demo.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.demo.servicebase.exception.AException;
import org.apache.ibatis.annotations.One;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.xml.UtilNamespaceHandler;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author liujiang
 * @since 2020-11-30
 */
//添加课程分类
//poi读取Excel内容
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void importSubjectDta(MultipartFile file,EduSubjectService eduSubjectService) {
        try{
            //1 获取文件流
            InputStream inputStream = file.getInputStream();

            //指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, ExcelSubjectData.class,new EduSubjectExcelLister(eduSubjectService)).sheet().doRead();

        }catch (Exception e){
            e.printStackTrace();
            throw new AException(20001,"添加课程异常");
        }
    }

    //获取所有嵌套课程
    @Override
    public List<OneSubject> nestedList() {

        //获取一级分类课程数据
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<EduSubject> eduOneSubjects = baseMapper.selectList(queryWrapper);

        //获取二级分类课程数据
        QueryWrapper<EduSubject> queryWrapperTwo = new QueryWrapper<>();
        queryWrapperTwo.ne("parent_id",0);
        List<EduSubject> eduTwoSubjects = baseMapper.selectList(queryWrapperTwo);


        List<OneSubject> oneFinalSubject  = new ArrayList<>();
        //将EduSubject数据转换成OneSubject
        for (int i = 0; i < eduOneSubjects.size(); i++) {
            EduSubject eduOneSubject = eduOneSubjects.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduOneSubject,oneSubject);

            List<TwoSubject> twoFinalSubject = new ArrayList<>();
            //转换二级分类的数据
            for (int m = 0; m < eduTwoSubjects.size(); m++) {
                EduSubject eduTwoSubject = eduTwoSubjects.get(m);
                TwoSubject twoSubject = new TwoSubject();
                BeanUtils.copyProperties(eduTwoSubject,twoSubject);
                if (eduOneSubject.getId().equals(eduTwoSubject.getParentId())){
                    twoFinalSubject.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinalSubject);
            oneFinalSubject.add(oneSubject);
        }

        return oneFinalSubject;
    }
}
